package com.fujimana.android.manacode.converter.impl;

import java.io.IOException;

import com.fujimana.android.manacode.converter.Options;
import com.fujimana.android.manacode.converter.Token;
import com.fujimana.android.manacode.converter.TokenAccessor;
import com.fujimana.android.manacode.converter.TokenMatcher;
import com.fujimana.android.manacode.converter.token.CommentToken;

public class SlashStarComentMatcher extends TokenMatcher {

	private StringBuilder sb;
	
	protected SlashStarComentMatcher(Options options) {
		super(options);
		this.sb = new StringBuilder();
	}

	@Override
	public boolean isMatch(String token, TokenAccessor accessor) throws IOException {
		if(isStart(token, accessor)) {
			String word = null;
			this.sb.append("/*");
			
			while(true) {
				word = accessor.next();
				
				if(isEnd(word, accessor)) {
					break;
				}
				
				sb.append(word);
			}
			
			sb.append("*/");
			
			return true;
		} else {
			return false;
		}
		
	}
	
	private boolean isEnd(String token, TokenAccessor accessor) throws IOException {
		
		String token2 =  accessor.next();
		if(token.equals("*") && token2.equals("/")) {
			return true;
		} else {
			accessor.pushBack();
			return false;
		}
	}
	
	private boolean isStart(String token, TokenAccessor accessor) throws IOException {
		return (token.equals("/") && accessor.next().equals("*"));
	}

	@Override
	public String getMatchString() {
		return sb.toString();
	}

	@Override
	public Token getToken(String token) {
		sb = new StringBuilder();
		return new CommentToken(token, getOptions());
	}

}
